/*
 * @Author: zhao_x601 1305443047@qq.com
 * @Date: 2022-06-30 11:08:53
 * @LastEditors: byhzdeMacBook-Pro-2.local
 * @LastEditTime: 2022-08-21 17:09:47
 * @FilePath: /demo_important/src/utils/windowUtils.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
// import Moment from 'moment'
const windowUtils = {};
/**
 * @description: 导出txt文件
 * @param {*} data 文件内容
 * @param {String} name 文件名称
 * @return {*}
 */
windowUtils.exportTxtPointData = (data, name) => {
  let urlObject = window.URL || window.webkitURL || window;
  let export_blob = new Blob([data]);
  let save_link = document.createElement("a");
  save_link.href = urlObject.createObjectURL(export_blob);
  save_link.download = name;
  save_link.click();
};

/**
 * @description: 获取月份的月初月末时间
 * @param {String} val 2020-02-02 || 2020/02/02
 * @return {Object} {startTime:'月初',endTime:"月末"}
 */
windowUtils.getMonthRange = (val) => {
  const objDate = {};
  const dateList = new Date(val).toLocaleDateString().split("/");
  objDate.startTime = dateList[0] + "/" + dateList[1] + "/" + dateList[2];
  objDate.endTime =
    dateList[0] +
    "/" +
    dateList[1] +
    "/" +
    new Date(dateList[0], dateList[1], 0).getDate();
  return objDate;
}

/**
 * @description: 获取月份时间戳
 * @param {String} val 2020-02-02 || 2020/02/02
 * @return {*}
 */
windowUtils.getDateStamp = (val,type) => {
  type = type ? type :0
  const dateList = new Date(val).toLocaleDateString().split("/");
  let stamp = dateList[0] + "-" + dateList[1] + "-" + dateList[2];
  stamp = type === 0 ? stamp + ' 00:00' : stamp + ' 23:59:59'
  return new Date(stamp).valueOf();
}

/**
 * @description: 获取定义的时间格式
 * @param {String} val 2020-02-02 || 2020/02/02
 * @param {String} type 间隔符号默认为空 20200202
 * @return {*}
 */
windowUtils.getDateDefine = (val, type) => {
  type = type || "";
  const dateList = new Date(val).toLocaleDateString().split("/");
  const stamp = dateList[0] + type + dateList[1];
  return stamp;
}

/**
 * @description: 时间戳转日期
 * @param {*} time
 * @return {*}
 */
windowUtils.changeTimeFormat = (time) => {
  var date = new Date(time);
  var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
  var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
  var hh = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
  var mm = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
  return date.getFullYear() + "-" + month + "-" + currentDate+" "+hh + ":" + mm;
  //返回格式：yyyy-MM-dd hh:mm
}

windowUtils.getMonthBetween = (start, end) => {
  var result = [];
  var s = start.split("-");
  var e = end.split("-");
  var min = new Date();
  var max = new Date();
  var yearMonthCode;
  var yearMonth;
  min.setFullYear(s[0], s[1] * 1 - 1, 1);//开始日期
  max.setFullYear(e[0], e[1] * 1 - 1, 1);//结束日期
  var curr = min;
  while (curr <= max) {
      yearMonthCode = Moment(curr).format('YYYYMM')
      var month = curr.getMonth();
      var year = curr.getFullYear();

      var str = curr.getFullYear() + "-" + (month);
      var s = curr.getFullYear() + "-0";
      if (str == s) {
          str = curr.getFullYear() + "-1";
      }
      var m = month + 1
      result.push({
          yearMonthCode: yearMonthCode,
          yearMonth: year + '年' + m + '月'
      });
      curr.setMonth(month + 1);
  }
  return result;
}
export default windowUtils;
